package com.fangcun.controller.api.old;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fangcun.bean.api.user.request.UserAddressListRequest;
import com.fangcun.bean.sys.ResponseMsg;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProAffairException;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.util.PublicMethod;
import com.fangcun.common.util.apiversion.ApiVersion;
import com.fangcun.common.util.base.BaseController;
import com.fangcun.entity.UserReceivingAddress;
import com.fangcun.service.api.UserAddressService;

/**
 * @作者： 陳曉鬆 @时间： 2020年7月16日 - 上午9:42:01 @description：->
 */
@RestController
@RequestMapping("/api/{version}/auth/user/address")
public class UserAddressController extends BaseController {

	private static Logger LOG = LoggerFactory.getLogger(UserAddressController.class);

	@Resource(name = "userAddressService")
	private UserAddressService userAddressService;

	@ApiVersion(1.0)
	@RequestMapping(value = "/add")
	private ResponseMsg add(@RequestBody UserReceivingAddress address) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			address.setUserId(getApiUserId());
			msg.setData(userAddressService.add(address));
		} catch (ProAffairException e) {
			LOG.error("-用户收货地址-添加出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-用户收货地址-添加异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "/update")
	private ResponseMsg update(@RequestBody UserReceivingAddress address) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			address.setUserId(getApiUserId());
			msg.setData(userAddressService.update(address));
		} catch (ProAffairException e) {
			LOG.error("-用户收货地址-修改出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-用户收货地址-修改异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "/list")
	private ResponseMsg list(@RequestBody UserAddressListRequest request) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.setUserId(getApiUserId());
			msg.setData(userAddressService.list(request));
		} catch (ProAffairException e) {
			LOG.error("-用户收货地址-查询出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-用户收货地址-查询异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	@ApiVersion(1.0)
	@RequestMapping(value = "/delete")
	private ResponseMsg delete(Long id) throws ProException {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			userAddressService.delete(id);
		} catch (ProAffairException e) {
			LOG.error("-用户收货地址-删除出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-用户收货地址-删除异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
